<template>
  <el-container class="admin-layout">
    <!-- 头部 -->
    <el-header class="admin-header">
      <div class="header-content">
        <div class="logo">
          <img src="/favicon.ico" alt="校事达" class="logo-img">
          <span class="logo-text">校事达管理系统</span>
        </div>
        
        <div class="header-right">
          <div class="user-info">
            <el-dropdown @command="handleUserCommand">
              <div class="user-dropdown">
                <el-avatar :src="getAvatarUrl(userStore.userInfo?.avatar)" :size="32">
                  {{ userStore.userInfo?.name?.charAt(0) }}
                </el-avatar>
                <span class="user-name">{{ userStore.userInfo?.name }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px" class="admin-sidebar">
        <el-menu
          :default-active="$route.path"
          router
          class="sidebar-menu"
        >
          <el-menu-item index="/admin/index">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-sub-menu index="user-management">
            <template #title>
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/admin/users">用户列表</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="task-management">
            <template #title>
              <el-icon><List /></el-icon>
              <span>任务管理</span>
            </template>
            <el-menu-item index="/admin/tasks">任务列表</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="school-management">
            <template #title>
              <el-icon><School /></el-icon>
              <span>学校管理</span>
            </template>
            <el-menu-item index="/admin/schools">学校列表</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="income-management">
            <template #title>
              <el-icon><Money /></el-icon>
              <span>收入管理</span>
            </template>
            <el-menu-item index="/admin/income-pending">待结算收入</el-menu-item>
            <el-menu-item index="/admin/income-settled">已结算收入</el-menu-item>
            <el-menu-item index="/admin/income-total">收入统计</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="admin-management">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>管理员管理</span>
            </template>
            <el-menu-item index="/admin/admin-list">管理员列表</el-menu-item>
            <el-menu-item index="/admin/add-admin">添加管理员</el-menu-item>
            <el-menu-item index="/admin/admin-password">密码管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="admin-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAvatarUrl } from '../../utils/avatar'

const router = useRouter()
const userStore = useUserStore()

const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人信息功能开发中...')
      break
    case 'settings':
      ElMessage.info('系统设置功能开发中...')
      break
    case 'logout':
      ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userStore.logout()
        router.push('/admin/login')
        ElMessage.success('已退出登录')
      })
      break
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.admin-header {
  background: #2c3e50;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.logo-img {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.logo-text {
  color: white;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background: rgba(255,255,255,0.1);
}

.user-name {
  color: white;
  font-size: 14px;
}

.admin-sidebar {
  background: #f5f5f5;
  border-right: 1px solid #e6e6e6;
}

.sidebar-menu {
  border: none;
  height: 100%;
}

.admin-main {
  background: #f8f9fa;
  padding: 20px;
}
</style>
